/*
 * 预付卡数据分析界面
 * 
 * 作图还需美化
 * 考虑是否能作多种图？eg：柱状图 etc
 * 
 */


package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import org.jfree.data.time.TimeSeriesCollection;

import dao.GetTable;

import exception.ViewException;

import test.DrawBalance;
import view.jfreechartTest.TimeSeriesExample;

public class PrePaymentAnalysisPanel extends JPanel {

	private static final long serialVersionUID = 2553377220376390288L;
	
	private JPanel singleStatisticPanel = new JPanel(new BorderLayout());
	private JButton singleGraphBn = new JButton("绘制图像");
	private JLabel cardNoJl = new JLabel("卡号：");
	private JTextField cardNoJtf = new JTextField();

	private JPanel totalStatisticPanel = new JPanel(new BorderLayout());
	private JLabel quaryType = new JLabel("查询功能选择:     选择数据表");
	private JComboBox tableList = new JComboBox();
	private JCheckBox rechargeButton = new JCheckBox("收入");
	private JCheckBox payButton = new JCheckBox("支出");
	private JCheckBox balanceButton = new JCheckBox("余额");
	private JCheckBox quaryButton = new JCheckBox("其它查询");

	private JButton totalGraphBn = new JButton("绘制图像");

	public PrePaymentAnalysisPanel() {
		setLayout(new GridLayout(5, 2));
		setBorder(BorderFactory.createTitledBorder("预付卡交易数据统计"));
		initSingleStatisticPanel();
		initTotalStatisticPanel();
		//add(singleStatisticPanel);
		add(totalStatisticPanel);
	}

	// 预付卡统计图像界面
	private void initSingleStatisticPanel() {

		singleStatisticPanel
				.setBorder(BorderFactory.createTitledBorder("单卡余额"));

		singleStatisticPanel.add(cardNoJl, BorderLayout.WEST);
		singleStatisticPanel.add(cardNoJtf);
		singleStatisticPanel.add(singleGraphBn, BorderLayout.EAST);
		// panel2.add(singleStatisticPanel, BorderLayout.NORTH);

		singleGraphBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				
				try {
					//TODO: get tableName
					String tableName = "networkpay";
					paintGraphForSingleCard(tableName, cardNoJtf.getText());
				} catch (ViewException e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(singleGraphBn, e.getMessage());
				}
				
			}
		});
	}

	// 预付卡界面-- 全卡分析
	private void initTotalStatisticPanel() {
		totalStatisticPanel.setBorder(BorderFactory.createTitledBorder("全卡余额"));

		rechargeButton.setMnemonic(KeyEvent.VK_C);
		rechargeButton.setSelected(true);

		payButton.setMnemonic(KeyEvent.VK_G);
		payButton.setSelected(true);

		balanceButton.setMnemonic(KeyEvent.VK_H);
		balanceButton.setSelected(true);

		quaryButton.setMnemonic(KeyEvent.VK_T);
		quaryButton.setSelected(true);

		tableList.addPopupMenuListener(new PopupMenuListener() {

			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
				if(e.getSource() == tableList) {
					tableList.removeAllItems();
					ArrayList<String> tables;
					try {
						tables = GetTable.getTable();
						for(int i = 0; i < tables.size(); i++) {
							tableList.addItem(tables.get(i));
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					tableList.setVisible(true);
				}
				
			}

			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				tableList.setVisible(true);
			}

			public void popupMenuCanceled(PopupMenuEvent e) {
				tableList.setVisible(true);
			}
			
		});
		tableList.setEditable(false);
		
		totalStatisticPanel.setLayout(new FlowLayout());
		totalStatisticPanel.add(quaryType);
		totalStatisticPanel.add(tableList);
		totalStatisticPanel.add(rechargeButton);
		totalStatisticPanel.add(payButton);
		totalStatisticPanel.add(balanceButton);
		totalStatisticPanel.add(quaryButton);
		totalStatisticPanel.add(totalGraphBn);

		totalGraphBn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				try{
					String tableName = tableList.getSelectedItem().toString();
					paintGraphFortotalGraph(tableName);
				}catch(Exception e) {
					
				}
			}
		});
	}

	//绘制图像
	private void paintGraphForSingleCard(String tableName,String cardNo) throws ViewException{
		// 检查cardNo是否合法

		// 然后查询数据 和 绘制图像
		String chartName = "单卡余额变化图";
		String legendName = "单卡余额";
		//DrawXYFromDatabase newChart = new DrawXYFromDatabase(cardNo,chartName,legendName);
		DrawBalance.drawSingleBalance(tableName,cardNo);
		
	}

	private void paintGraphFortotalGraph(String tableName) {
		// TODO Auto-generated method stub
		String quaryCondition = "";
		int quarySelect = 1;
		int selectCount = 0;
		if (rechargeButton.isSelected()) {
			quaryCondition = quaryCondition + " rechargeSelected ";
			quarySelect = quarySelect * 2;
			selectCount++;
		}
		if (payButton.isSelected()) {
			quaryCondition = quaryCondition + " paySelected ";
			quarySelect = quarySelect * 3;
			selectCount++;
		}
		if (balanceButton.isSelected()) {
			quaryCondition = quaryCondition + " balanceSelected ";
			quarySelect = quarySelect * 5;
			selectCount++;
		}
		if (quaryButton.isSelected()) {
			quaryCondition = quaryCondition + " quarySelected ";
			quarySelect = quarySelect * 7;
			selectCount++;
		}
		if (quaryCondition == "") {
			System.out.println("at least select one!");
		} else {
			//TODO: 区分不同的选择画不同类型的图，封装为一个函数进行操作
			//封装函数在DrawXYFromDatabase.java中
			//参数：selectCount,quarySelect
			DrawXYFromDatabase newChart = new DrawXYFromDatabase();
			if(selectCount == 1) {
				newChart.DrawSingleChart(quarySelect, tableName);
			}
			//多图作在一张图上
			else {
				newChart.DrawMultiChart(quarySelect, tableName);
			}
			/*
			String chartName = "整体金额变化情况";
			// 设置画图参数
			TimeSeriesExample tse = new TimeSeriesExample(chartName);
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			dataset = newChart.drawXYPlotFromResultSet(quarySelect);
			tse.DrawXYChart(dataset, chartName);*/
		}

	}
}
